package org.nidy.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.nidy.erp.pojo.MemberShip;
import org.nidy.erp.service.IMemberShipService;
import org.nidy.erp.utils.PageBean;
import org.nidy.erp.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/memberShip")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MemberShipController {
    @Autowired
    private IMemberShipService memberShipService;
    @PostMapping("/getMemberList")
    public ResponseResult getMemberList(@RequestBody PageBean pageBean) {
        String query = pageBean.getQuery().trim();
        Page<MemberShip> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<MemberShip> pageResult = memberShipService.page(page, new QueryWrapper<MemberShip>().like(StringUtils.isNotEmpty(query),"nickName" , query));
        Map<String,Object> map=new HashMap<>();
        map.put("userList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return ResponseResult.success(map);
    }

    @PostMapping("/getMemberListByNickName")
    public ResponseResult getMemberListByNickName(@RequestBody PageBean pageBean) {
        String query = pageBean.getQuery().trim();
        if(!query.isEmpty() &&  query != null){
            List<MemberShip> memberListByNickName = memberShipService.getMemberListByNickName(query);
            Map<String,Object> map=new HashMap<>();
            map.put("userList",memberListByNickName);
            return ResponseResult.success(map);
        }else {
            ResponseResult memberList = this.getMemberList(pageBean);
            return memberList;
        }

    }




}
